package com.yanqu.road.dao.impl.activity.goodsLimit;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.goodsLimit.GoodsLimitData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class GoodsLimitDataDaoImpl extends TempDataObjectDao<GoodsLimitData> {

    public static String getGoodsLimitDataSql = "select * from `t_u_goods_limit_data` where `userId`=? ";

    public Map<Integer, GoodsLimitData> getGoodsLimitDataMap(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, GoodsLimitData> map = new HashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getGoodsLimitDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                GoodsLimitData data = parse(rs);
                map.put(data.getGoodsId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateGoodsLimitDataSql = "update `t_u_goods_limit_data` set activityId=?,count=?,updateTime=? where userId=? and goodsId=?";

    @Override
    protected boolean updateData(GoodsLimitData goodsLimitData) {
        goodsLimitData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, goodsLimitData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, goodsLimitData.getCount()));
        para.put(i++, new DbParameter(Types.BIGINT, goodsLimitData.getUpdateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, goodsLimitData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, goodsLimitData.getGoodsId()));
        if (getDb().executeNoneQuery(updateGoodsLimitDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertGoodsLimitDataSql = "insert into `t_u_goods_limit_data`(userId,goodsId,activityId,count,updateTime) values (?,?,?,?,?)";

    @Override
    protected boolean addData(GoodsLimitData goodsLimitData) {
        goodsLimitData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.BIGINT, goodsLimitData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, goodsLimitData.getGoodsId()));
        para.put(i++, new DbParameter(Types.INTEGER, goodsLimitData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, goodsLimitData.getCount()));
        para.put(i++, new DbParameter(Types.BIGINT, goodsLimitData.getUpdateTime()));
        if (getDb().executeNoneQuery(insertGoodsLimitDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected GoodsLimitData parseData(ResultSet rs) throws SQLException {
        GoodsLimitData goodsLimitData = new GoodsLimitData();
        goodsLimitData.setUserId(rs.getLong("userId"));
        goodsLimitData.setGoodsId(rs.getInt("goodsId"));
        goodsLimitData.setActivityId(rs.getInt("activityId"));
        goodsLimitData.setCount(rs.getLong("count"));
        goodsLimitData.setUpdateTime(rs.getLong("updateTime"));

        return goodsLimitData;
    }


}
